﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JohnnySins.Entities;
using Dapper;

namespace JohnnySins.DAL.Repositories
{
    public class NextLiveShowRepository : BaseRepository<NextLiveShow>
    {
        public IEnumerable<NextLiveShow> FindAll()
        {
            return Connection.Query<NextLiveShow>(@"SELECT rs.NextLiveShowId, rs.NextLiveShowName, rs.NextLiveShowDate, rs.IsActive
                    FROM [NextLiveShow] (nolock) rs");
        }

        public IEnumerable<NextLiveShow> FindActive()
        {
            return Connection.Query<NextLiveShow>(@"SELECT rs.NextLiveShowId, rs.NextLiveShowName, rs.NextLiveShowDate, rs.IsActive
                    FROM [NextLiveShow] (nolock) rs where (rs.IsActive = 1)");
        }

        public override NextLiveShow Find(int id)
        {
            return Connection.Query<NextLiveShow>(
                @"select NextLiveShowId, NextLiveShowName, NextLiveShowDate, IsActive
                from [NextLiveShow]
                where NextLiveShowId = @NextLiveShowId", new { NextLiveShowId = id }).SingleOrDefault();
        }

        public override void Insert(NextLiveShow nextLiveShow)
        {
            int result = Connection.Query<int>(
                @"INSERT INTO [NextLiveShow] (NextLiveShowName, NextLiveShowDate, IsActive) 
                VALUES (@NextLiveShowName, @NextLiveShowDate, @IsActive); SELECT CAST(SCOPE_IDENTITY() AS INT);", nextLiveShow).Single();
            nextLiveShow.NextLiveShowId = result;
        }

        public override void Update(NextLiveShow nextLiveShow)
        {
            Connection.Execute(@"update [NextLiveShow] 
                set NextLiveShowName = @NextLiveShowName, NextLiveShowDate = @NextLiveShowDate, IsActive = @IsActive
                where NextLiveShowId = @NextLiveShowId", nextLiveShow);
        }
        
        public void Activate(int id)
        {
            Connection.Execute(
                @"update [NextLiveShow] set IsActive = 1
                where NextLiveShowId = @NextLiveShowId", new { NextLiveShowId = id });
        }

        public void Deactivate(int id)
        {
            Connection.Execute(
                @"update [NextLiveShow] set IsActive = 0
                where NextLiveShowId = @NextLiveShowId", new { NextLiveShowId = id });
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete from [NextLiveShow]
                    where NextLiveShowId = @NextLiveShowId", new { NextLiveShowId = id });
        }
    }
}
